package com.car.rental.user.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.lang.NonNull;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    private static final Logger log = LoggerFactory.getLogger(WebConfig.class);
    
    @Value("${upload.base-path}")
    private String basePath;
    
    @Value("${upload.paths.avatar}")
    private String avatarPath;
    
    @Value("${upload.paths.license}")
    private String licensePath;

    @Override
    public void addResourceHandlers(@NonNull ResourceHandlerRegistry registry) {
        // 配置头像访问
        String avatarLocation = "file:" + basePath + avatarPath;
        File uploadDir = new File(basePath + avatarPath);
        log.info("实际文件存储路径: {}", uploadDir.getAbsolutePath());
        
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
            log.info("创建上传目录: {}", uploadDir.getAbsolutePath());
        }
        
        registry.addResourceHandler("/user/uploads/avatar/**")
                .addResourceLocations(avatarLocation)
                .setCachePeriod(3600)
                .resourceChain(true);
        
        log.info("静态资源映射配置 - 访问路径: /user/uploads/avatar/**, 实际路径: {}", avatarLocation);
        
        // 配置驾驶证照片访问
        String licenseLocation = "file:" + basePath + licensePath;
        registry.addResourceHandler("/user/uploads/license/**")
                .addResourceLocations(licenseLocation)
                .setCachePeriod(3600)
                .resourceChain(true);
    }
} 